package net 
{
	import binary.QhByteArray;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.Socket;
	
	/**
	 * socket断开事件
	 */
	[Event(name = "close", type = "flash.events.Event")]
	
	/**
	 * socket建立连接事件
	 */
	[Event(name = "connect", type = "flash.events.Event")]
	
	/**
	 * socket安全沙箱
	 */
	[Event(name = "securityError", type = "flash.events.SecurityErrorEvent")]
	
	/**
	 * IO错误
	 */
	[Event(name = "ioError", type = "flash.events.IOErrorEvent")]
	
	/**
	 * 封装的socket连接类
	 * @author gcxk721
	 */
	public class SocketConnect extends EventDispatcher
	{
		
		/**
		 * socket连接
		 */
		protected var socket : Socket = new Socket(null, 0);
		
		/**
		 * 消息缓存处理类
		 */
		protected var filter : IDataFilter;
		
		public function SocketConnect(handler : IMessageHandler) 
		{
			filter = new QhDataFilter(handler);
			socket.addEventListener(Event.CLOSE, onCloseHandler);
			socket.addEventListener(Event.CONNECT, onConnectHandler);
			socket.addEventListener(ProgressEvent.SOCKET_DATA, onDataHandler, false, 999, true);
			socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityErrorHandler);
			socket.addEventListener(IOErrorEvent.IO_ERROR, onIoErrorHandler);
		}
		
		/**
		 * socket取得数据
		 * @param	evt
		 */
		protected function onDataHandler(evt : ProgressEvent) : void
		{
			var buf : QhByteArray = new QhByteArray();
			socket.readBytes(buf, 0, socket.bytesAvailable);
			filter.onRecive(buf);
		}
		
		/**
		 * 连接成功的处理函数
		 * @param	evt
		 */
		protected function onConnectHandler(evt : Event) : void
		{
			evt.stopPropagation();
			dispatchEvent(evt);
		}
		
		/**
		 * 沙箱冲突
		 * @param	evt
		 */
		protected function onSecurityErrorHandler(evt : SecurityErrorEvent) : void
		{
			evt.stopPropagation();
			dispatchEvent(evt);
		}
		
		/**
		 * IO错误
		 * @param	evt
		 */
		protected function onIoErrorHandler(evt : IOErrorEvent) : void
		{
			evt.stopPropagation();
			dispatchEvent(evt);
		}
		
		/**
		 * socket关闭的处理函数
		 * @param	evt
		 */
		protected function onCloseHandler(evt : Event) : void
		{
			evt.stopPropagation();
			dispatchEvent(evt);
		}
		
		/**
		 * 通过socket发送消息
		 * @param	bytes
		 */
		public function send(bytes : QhByteArray) : void
		{
			if (socket.connected) 
			{
				socket.writeInt(bytes.length - 2);
				socket.writeBytes(bytes, 0, bytes.length);			
				socket.flush();
			}
		}
		
		/**
		 * 取得当前的socket是否已经连接
		 */
		public function get connected() : Boolean
		{
			return socket.connected;
		}
		
		/**
		 * 设置socket的连接重置时间
		 * @param	value
		 */
		public function setTimeout(value : uint) : void
		{
			socket.timeout = value;
		}
		
		/**
		 * socket进行连接的函数
		 * @param	host
		 * @param	port
		 */
		public function connect(host : String, port : int) : void
		{
			socket.connect(host, port);
		}
		
		/**
		 * socket断开连接
		 */
		public function logout() : void
		{
			if (socket.connected)
			{
				socket.close();
			}
		}
		
	}

}